<script setup lang="ts">
import { getConsultOrderList } from '@/api/consult'
import ConsultItem from './ConsultItem.vue'
import { ref } from 'vue'
import type { ConsultType } from '@/enums'
import type { ConsultOrderItem } from '@/types/consult'
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)

// 接收问诊类型：1问医生2极速问诊3开药问诊
const props = defineProps<{
  type: ConsultType
}>()
// 请求的参数
const params = {
  current: 1, // 当前页码
  pageSize: 10, // 每页条数
  type: props.type // 问诊类型
}
const onLoad = () => {
  // 异步更新数据
  // setTimeout 仅做示例，真实场景中一般为 ajax 请求
  setTimeout(async () => {
    // for (let i = 0; i < 10; i++) {
    //   list.value.push(list.value.length + 1)
    // }
    const { rows, total } = await getConsultOrderList(params)
    // 追加当前页的数据
    list.value.push(...rows)

    // 加载状态结束
    loading.value = false

    // 数据全部加载完成
    if (list.value.length === total) {
      // 数据加载完了
      finished.value = true
    } else {
      // 还有数据，页码需要加一
      params.current++
    }
  }, 1000)
}
// 删除订单
const onDelete = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <consult-item @on-delete="onDelete" v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
